layui.use(['table', 'admin', 'ax','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;

    /**
     * 档案销毁信息表管理
     */
    var ArchiveDestruction = {
        tableId: "archiveDestructionTable"
    };

    /**
     * 初始化表格的列
     */
    ArchiveDestruction.initColumn = function () {
        return [[
            {type: 'radio'},
            {field: 'id', hide: true, title: 'id'},
            {field: 'xhdh', sort: true, title: '销毁单号'},
            {field: 'jbr', sort: true, title: '经办人'},
            {field: 'jysj', sort: true, title: '销毁时间'},
            {field: 'beiz', sort: true, title: '备注'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    ArchiveDestruction.search = function () {
        var condi = $.extend({},form.val('searchForm'));
        table.reload(ArchiveDestruction.tableId, {
            where: condi, page: {curr: 1}
        });
    };

    //表单提交事件
    form.on('submit(btnSearch)', function (data) {
        ArchiveDestruction.search();
        return false;
    });


    /**
     * 弹出添加对话框
     */
    ArchiveDestruction.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/archiveDestruction/add';
    };

    /**
     * 导出excel按钮
     */
    ArchiveDestruction.exportExcel = function () {
        var checkRows = table.checkStatus(ArchiveDestruction.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    ArchiveDestruction.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/archiveDestruction/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    ArchiveDestruction.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/archiveDestruction/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(ArchiveDestruction.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ArchiveDestruction.tableId,
        url: Feng.ctxPath + '/archiveDestruction/list',
        page: true,
        height: "500",
        cellMinWidth: 100,
        cols: ArchiveDestruction.initColumn()
    });


    table.on('row(' + ArchiveDestruction.tableId + ')', function (obj) {
        ArchiveInfo.reloadArchive(obj.data.id);
    })

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        ArchiveDestruction.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        ArchiveDestruction.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + ArchiveDestruction.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            ArchiveDestruction.openEditDlg(data);
        } else if (layEvent === 'delete') {
            ArchiveDestruction.onDeleteItem(data);
        }
    });




    //已销毁档案列表

    //借阅的档案列表
    /**
     * 档案信息表管理
     */
    var ArchiveInfo = {
        tableId: "archiveDestroyArchiveTable"
    };

    /**
     * 初始化表格的列
     */
    ArchiveInfo.initColumn = function () {
        return [[
            {field: 'wjid', width: 100, sort: true, title: '卷宗号'},
            {field: 'mc',   width: 120, title: '档案名称'},
            {field: 'dalx',   title: '档案类型',templet:'#dalx'},
            {field: 'ssrrmc', title: '所属人'},
            {field: 'mj',     title: '密级',templet:'#mj'},
            {field: 'glnx',   title: '管理年限',templet:'#glnx'},
            {field: 'nd',     title: '年度'},
            {field: 'ly',     title: '来源',templet:'#ly'},
            {field: 'bz',     title: '备注'},
            {field: 'sfyx',   title: '是否有效',templet:'#sfyx'},
        ]];
    };

    // 渲染表格
    table.render({
        elem: '#' + ArchiveInfo.tableId,
        data: [],
        page: false,
        height: "616",
        cellMinWidth: 100,
        cols: ArchiveInfo.initColumn()
    });

    ArchiveInfo.reloadArchive = function (parentId) {
        table.reload(ArchiveInfo.tableId, {
            url: Feng.ctxPath + '/archiveInfo/listDestroy?id=' + parentId,
        });
    };
});
